import { DataTypes } from 'sequelize';
import { Model } from './common.js';

// Extend the Model class with the attributes interface
export class FilmMultimediaRelation extends Model {
  // 主键ID
  static PRIMARY_KEY = 'id';

  // 待发布
  static RELEASE_STATUS_WAIT = 0;
  // 发布中
  static RELEASE_STATUS_PROGRESS = 1;
  // 发布成功
  static RELEASE_STATUS_SUCCESS = 2;
  // 发布失败
  static RELEASE_STATUS_FAIL = 3;
  // 已删除
  static RELASE_STATUS_DELETE = 4;
  // 抖音端删除或者不宜公开
  static RELASE_STATUS_PLATFORM_DELETE = 5;
  // 已预约发布
  static RELASE_STATUS_APPOINT = 6;
  // 排队中（内部）
  static RELEASE_STATUS_JOB = 7;
}

export async function checkAndAddFields(sequelize) {
  const tableDescription = await FilmMultimediaRelation.describe();

  if (!tableDescription.extra_info) {
    await sequelize.getQueryInterface().addColumn('film_multimedia_relation', 'extra_info', {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '额外信息',
    });
  }
}

export function initFilmMultimediaRelation(sequelize) {
  FilmMultimediaRelation.init(
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
      },
      org_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '机构id',
      },
      user_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '用户ID'
      },
      film_release_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '关联: film_release(一对多)'
      },
      platform_account_user_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '关联表: platform_account_user'
      },
      title: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '标题',
      },
      caption: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: '简介',
      },
      multimedia: {
        type: DataTypes.JSON,
        allowNull: false,
        comment: '视频信息',
      },
      hashtag_name: {
        type: DataTypes.JSON,
        allowNull: true,
        comment: '话题',
      },
      visibility_type: {
        type: DataTypes.TINYINT,
        defaultValue: 0,
        comment: '设置谁可以看：0 公开 1 仅自己可见 2 仅好友可见'
      },
      download: {
        type: DataTypes.TINYINT,
        defaultValue: 1,
        comment: '允许他人保存视频：1允许 0不允许'
      },
      anchor: {
        type: DataTypes.JSON,
        allowNull: true,
        comment: '挂载信息',
      },
      associ_poi: {
        type: DataTypes.JSON,
        allowNull: true,
        comment: 'poi信息',
      },
      release_status: {
        type: DataTypes.TINYINT,
        allowNull: false,
        defaultValue: 0,
        comment: '发布状态：0-待发布，1-发布中，2-发布成功，3-发布失败，4-已删除，5-抖音端删除或者不宜公开',
      },
      api_failed: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '发布失败的原因',
      },
      release_reason: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '发布状态信息（完整记录发布返回报错）',
      },
      api_result: {
        type: DataTypes.JSON,
        allowNull: true,
        comment: '发布返回结果',
      },
      release_time: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: '发布时间',
      },
      platform: {
        type: DataTypes.TINYINT,
        allowNull: false,
        comment: '发布平台',
      },
      job_time: {
        type: DataTypes.DATE,
        allowNull: false,
        comment: '前往队列排队时间',
      },
      extra_info: {
        type: DataTypes.JSON,
        allowNull: true,
        comment: '额外信息',
      },
    },
    {
      sequelize,
      modelName: 'FilmMultimediaRelation',
      tableName: 'film_multimedia_relation',
      timestamps: false,
    }
  );

  // checkAndAddFields(sequelize);
  sequelize.sync()
    .then(() => {
      checkAndAddFields(sequelize);
    })
    .catch(error => {
      console.error('Error syncing the database:', error);
    });
}
